<?php
/**
 * 
 * 관리 controller
 * @author gudals
 *
 */

class contMngCt extends contController
{
    public $action = 'index';
    
    private $aCss = array(
                'name' => 'common',
                'file_list' => array('jquery.ui')
            );
    
    private $aJs = array(
                'name' => 'cont.mng',
                'file_list' => array('lib/jquery.ui', 'util/spin', 'util/util.form.validator', 'cont/cont.mng')
            );
    
    public function index()
    {
        // 각 페이지에 필요한 CSS, JS 파일만 적용할 것.
        //$this->initialize();
        commViewer::_css($this->aCss);
        commViewer::_js($this->aJs);
    
        // 레이아웃 표시 여부
        //$this->sTopLayoutView = false;
        //$this->sAsideLayoutView = false;
        //$this->sFooterLayoutView = false;
    
        // 팀 리스트
        $aTeamList = m('team_info')->getList();
        $aTeamInfo = array();
        for ($i = 0, $iLen = count($aTeamList); $i < $iLen; $i++) {
            $_row = $aTeamList[$i];
            $aTeamInfo[$_row['team_no']] = $_row['team_name'];
        }
        
        // 팀원 리스트
        $aTeamMemberList = m('team_member')->getList($iTeamNo);
    
        // data assign
        $aAssignData = array(
            'aTeamList' => $aTeamList,
            'aTeamInfo' => $aTeamInfo,
            'aTeamMemberList' => $aTeamMemberList
        );
    
        // display view
        $this->view('/cont/mng/index.html', $aAssignData);
    }
    
    /**
     * 
     * 팀 등록/수정
     */
    public function regTeamAct()
    {
        $aParam = $this->params;
        
        // 수정
        if ($aParam['modeTeam'] == 'modify') {
            $bResult = m('team_info')->update($aParam);
            if (empty($bResult)) {
                utilData::jsonSingle(201);
                exit;
            }
            
        // 등록
        } else {
            $iInsertId = m('team_info')->insert($aParam);
            if (empty($iInsertId)) {
                utilData::jsonSingle(201);
                exit;
            }
        }
        
        utilData::jsonSingle(200);
    }
    
    /**
     * 
     * 팀 삭제
     */
    public function delTeamAct()
    {
        $iTeamNo = $this->params['team_no'];
        
        $bResult = m('team_info')->delete($iTeamNo);
        if (empty($bResult)) {
            utilData::jsonSingle(201);
            exit;
        }
        
        utilData::jsonSingle(200);
    }
    
    /**
     * 
     * 팀원 등록
     */
    public function regTeamMemberAct()
    {
        $aParam = $this->params;
        
        $sMemberId = $aParam['member_id'];
        
        // 아이디 중복 체크
        $aInfo = m('team_member')->getMemberInfo($sMemberId);
        if (empty($aInfo['member_no']) === false) {
            utilData::jsonSingle(301);
            exit;
        }
        
        // 수정
        if ($aParam['modeMember'] == 'modify') {
            $bResult = m('team_member')->update($aParam);
            if (empty($bResult)) {
                utilData::jsonSingle(201);
                exit;
            }
            
        // 등록
        } else {
            $iInsertId = m('team_member')->insert($aParam);
            if (empty($iInsertId)) {
                utilData::jsonSingle(201);
                exit;
            }
        }
        
        utilData::jsonSingle(200);
    }
}